import {
  Request,Login
} from "../../utils/request.js";
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: getApp().globalData.statusBarHeight,
    open:1, //切换喜事 指南 我们
    opens:1,//切换安全防骗 情感心理 婚恋指导
    height:0,
    chooseSize:false,//一开始的动画属性
    chooseSort:false, //一开始的距离点击排序
    sliderVaule:1,//人均消费滑块的初始值
    priceStutas:true,
    slider:1,//搜索起步价
    doorType:[{id:1,name:"餐饮"},{id:2,name:"娱乐"},{id:3,name:"休闲"},{id:4,name:"运动"},{id:5,name:"购物"},{id:6,name:"其它"}],
    xishi_code: false,  //默认 不显示没有喜事列表
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //地址路径
    this.ConfigUrl =   wx.getStorageSync("config");
    /**
     * 文本内容列表加载
     */
    new Request('Zylove/getrichtext').request(
      () => {
        return {
          openid: app.globalData.openid,
        }
      },
      (res) => {
        res.data.msg.map(function(e,n,m){
          if(e.id == 1){
            // this.richone = e.rich;
            this.setData({
              richone:e.rich,
            });
          }
          if(e.id == 2){
            // this.richtwo = e.rich;
            this.setData({
              richtwo:e.rich,
            });
          }
          if(e.id == 3){
            // this.richthree = e.rich;
            this.setData({
              richthree:e.rich,
            });
          }
          if(e.id == 4){
            // this.richfour = e.rich;
            this.setData({
              richfour:e.rich,
            });
          }
          if(e.id == 5){
            // this.richfive = e.rich;
            this.setData({
              richfive:e.rich,
            });
          }
          if(e.id == 6){
            this.richsix = e.rich;
            this.setData({
              richsix:e.rich,
            });
          }
        }.bind(this));
      });
      /**
       * 获取商店列表List
       */
      new Request('Zylove/getshoplist').request(
        () => {
          return {
            openid: app.globalData.openid,
          }
        },
        (res) => {
          this.shopList = res.data.msg; //获取店铺列表数据
        }
      );

      
  },
  /**
   * 重新加载商店列表
   */
  chongxingshop:function(){
    /**
     * 获取商店列表List
     */
    new Request('Zylove/getshoplist').request(
      () => {
        return {
          openid: app.globalData.openid,
        }
      },
      (res) => {
        this.shopList = res.data.msg; //获取店铺列表数据
      }
    );
    this.getuseraddress();
  },
  /**
   * 
   * @param {渲染第一篇指南文章长度} res 
   */
  _one:function(){
    wx.showToast({
      title: '',
      icon: 'loading',
      duration: 350
    })
    setTimeout(function(){
      const query = wx.createSelectorQuery()
      query.select('#rich_one').boundingClientRect(function(res){
        console.log(res);
        var height = res.height;
        this.setData({
          height: height
        });
      }.bind(this)).exec();
    }.bind(this), 300);
  },
  /**
   * 
   * @param {渲染第一篇指南文章长度} res 
   */
  _ones:function(){
    wx.showToast({
      title: '',
      icon: 'loading',
      duration: 350
    })
    setTimeout(function(){
      const query = wx.createSelectorQuery()
      query.select('#rich_ones').boundingClientRect(function(res){
        console.log(res);
        var height = res.height;
        this.setData({
          height: height
        });
      }.bind(this)).exec();
    }.bind(this), 300);
  },
  /**
   * 安全防骗 / 情感心理 / 婚恋指导 切换滑动事件
   */
  _bindchange:function(res){
    //将页面滚动到目标位置，支持选择器和滚动距离两种方式定位
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 0
    });
    if(res.detail.current == 0){
      this.rich = '#rich_one';
      this.data.opens = 1;
    }else if(res.detail.current == 1){
      this.rich = '#rich_two';
      this.data.opens = 2;
    }else if(res.detail.current == 2){
      this.rich = '#rich_three';
      this.data.opens = 3;
    }
    wx.showToast({
      title: '加载中...',
      icon: 'loading',
      duration: 200
    })
    setTimeout(function(){
      wx.createSelectorQuery().select(this.rich).boundingClientRect(function(res){
        var height = res.height;
        this.setData({
          height: height,
          opens: this.data.opens,
        });
      }.bind(this)).exec();
    }.bind(this), 1);
   
  },
  /**
  * 产品功能 / 会员价格 / 品牌文化 切换滑动事件
  */
 _bindchanges:function(res){
   //将页面滚动到目标位置，支持选择器和滚动距离两种方式定位
   wx.pageScrollTo({
    scrollTop: 0,
    duration: 0
  });
   if(res.detail.current == 0){
     this.rich = '#rich_ones';
     this.data.opens = 1;
   }else if(res.detail.current == 1){
     this.rich = '#rich_twos';
     this.data.opens = 2;
   }else if(res.detail.current == 2){
     this.rich = '#rich_threes';
     this.data.opens = 3;
   }
   wx.showToast({
     title: '加载中...',
     icon: 'loading',
     duration: 200
   })
   setTimeout(function(){
     wx.createSelectorQuery().select(this.rich).boundingClientRect(function(res){
       var height = res.height;
       this.setData({
         height: height,
         opens: this.data.opens,
       });
     }.bind(this)).exec();
   }.bind(this), 1);
  
 },
 /**
   * 跳转到发布喜事页面
   */
  gotoadd:function(){
    //游客
    if(app.globalData.loginCode == -1){
      //用户未注册 提示跳转注册页面
      new Login().LoginDown();
      return;
    }
    wx.navigateTo({
      url: '/pages/zhaon_zhuangjiao/zhaon_zhuangjiao_fabu/zhaon_zhuangjiao_fabu',
      events: {
        // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
        acceptDataFromOpenedPage: function(data) {
          console.log(data)
        },
        someEvent: function(data) {
          console.log(data)
        }
      },
      success: function(res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('acceptDataFromOpenerPage', { data: 'test' })
      }
    })
  },
  /**
   * 
   * @param {点击用户头像 跳转用户基本信息} param 
   */
  gotoUserInfo:function(param){
    console.log(param.currentTarget.dataset.id)
    wx.navigateTo({
      url: '/pages/zhaon_friendinfo/zhaon_friendinfo?userid='+param.currentTarget.dataset.id,
    });
  },
  /**
   * 切换喜事 指南 我们
   */
  qietap:function(param){
    if(param.currentTarget.id == 1){
      this.data.open = 1;
      this.data.chooseSize = true;
      this.chooseSezi();//关闭筛选菜单
    }else if(param.currentTarget.id == 2){
      this._one();
      this.data.open = 2;
      this.data.chooseSize = true;
      this.chooseSezi();//关闭筛选菜单
    }else if(param.currentTarget.id == 3){
      this.data.open = 3;
      this._ones();
      this.data.chooseSize = true;
      this.chooseSezi();//关闭筛选菜单
    }else if(param.currentTarget.id == 4){
      this.data.open = 4;
      this.chongxingshop();
      // this.getuseraddress(); //获取用户当前位置
      
    }
    this.setData({
      open: this.data.open,
      opens:1,
      _current:0,
      slider:1,//清空筛选值
      chooseId:false,//清空筛选值
    });
  },
  /**
   * 切换安全防骗 情感心理 婚恋指导
   */
  qiehuantap:function(param){
    if(param.currentTarget.id == 1){
      this.data.opens = 1;
      let obj = new Object({detail:{current:0}});
      this._bindchange(obj);
    }else if(param.currentTarget.id == 2){
      this.data.opens = 2;
      let obj = new Object({detail:{current:1}});
      this._bindchange(obj);
    }else if(param.currentTarget.id == 3){
      let obj = new Object({detail:{current:2}});
      this._bindchange(obj);
      this.data.opens = 3;
    }
    this.setData({
      opens: this.data.opens,
      _current: (this.data.opens - 1),
    });
  },
  /**
   * 
   * @param { 产品功能 / 会员价格 / 品牌文化} param 
   */
  qiehuantaps:function(param){
    if(param.currentTarget.id == 1){
      this.data.opens = 1;
      let obj = new Object({detail:{current:0}});
      this._bindchanges(obj);
    }else if(param.currentTarget.id == 2){
      this.data.opens = 2;
      let obj = new Object({detail:{current:1}});
      this._bindchanges(obj);
    }else if(param.currentTarget.id == 3){
      let obj = new Object({detail:{current:2}});
      this._bindchanges(obj);
      this.data.opens = 3;
    }
    this.setData({
      opens: this.data.opens,
      _current: (this.data.opens - 1),
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let url = this.ConfigUrl;
  /**
   * 喜事列表加载
   */
  new Request('publish/Publish_List/GetList').request(
    () => {
      return {
        openid: app.globalData.openid,
      }
    },
    (res) => {
      if(res.data.code == 0){
        new Request().showToast('暂无喜事');
        this.setData({
          xishi_code: true,
        });
        return;
      }
      res.data.data.map((e)=>{
        e.publish_avatar  = url.service.host + e.publish_avatar;
        e.object_avatar   = url.service.host + e.object_avatar;
        e.publish_image   = url.service.host + e.publish_image;
      });
      this.setData({
        xishiList: res.data.data,
      });
    }
  );
    //从会员页面跳转进入到我们 会员服务页面
    if(app.globalData.click){
      this.setData({
        open:3,
        opens:2,
        _current:1,
        richfive:this.data.richfive,
      });
      let LogSetInterval = Array();
      let LogSetIntNum = 20;
      this.SetIntNumberUserInfo_One = setInterval(()=>{
          wx.createSelectorQuery().select('#rich_twos').boundingClientRect(function(res){
            console.log(res.height);
            var height = res.height;
            this.setData({
              height: height,
              opens: this.data.opens,
            });
          }.bind(this)).exec();
          LogSetInterval.push(1);
          if(LogSetInterval.length == LogSetIntNum){
            clearInterval(this.SetIntNumberUserInfo_One);  //关闭 铺获
          }
        },100);
    }
  },
  /**
   * 
   * @param {滑块选择器} e 
   */
  sliderchange:function(e){
    this.setData({
      slider:e.detail.value
    })
  },
  /**
   * 
   * @param {选择门店类型} e 
   */
  _doorType:function(e){
    this.Chooes_shopid = e.currentTarget.id;
    this.setData({
      chooseId:e.currentTarget.id
    });
  },
  /**
   * 
   * @param {重置筛选} e 
   */
  _reset:function(e){
    this.chooseSezi();//关闭 筛选面板
    this.setData({
      slider:1,
      chooseId:false,
      sliderVaule:1,
    });
    if(e) {
      wx.showToast({
        title: 'clear...',
        icon: 'loading',
        duration: 500
      });
      new Request('Zylove/getshoplist').request(
        () => {
          return {
            openid: app.globalData.openid,
          }
        },
        (res) => {
          this.shopList = res.data.msg; //获取店铺列表数据
          this.getuseraddress();
        }
      );
    }
  },
  /**
   * 
   * @param {确认筛选} e 
   */
  _confirm:function(e){
    this.chooseSezi();//关闭 筛选面板
    new Request('Zhaon_Get_Shop_List/GetChooesShopList').request(
      () => {
        return {
          openid: app.globalData.openid,
          shop_id: this.Chooes_shopid,
          shop_price: this.data.slider,
        }
      },
      (res) => {
        if(res.data.msg == '' || res.data.msg == null || res.data.data == -1){
          this.setData({
            shoplistData: ''
          });
          new Request().showToast('暂无商店列表');
          return;
        }
        this.setData({
          shoplistData: ''
        });
        this.shopList = res.data.msg; //获取店铺列表数据
        this.getuseraddress();
      });
  },
  /**
   * 
   * @param {点击透明背景图 关闭筛选面板 } e 
   */
  _clear:function(e){
    this.chooseSezi(); //关闭 筛选面板
    this._reset();  //重置筛选
  },
  /**
   * 
   * @param {获取用户当前地址} e 
   */
  getuseraddress:function(e){
    wx.getLocation({
      type: 'gcj02',
      success:function (res) {
        this.latitude = res.latitude
        this.longitude = res.longitude
        // const speed = res.speed
        // const accuracy = res.accuracy
        this.shopList.map(function(e,n,m){
          e.latlongaddress = this.getaddreskm(e.shopLat,e.shopLng,res.latitude,res.longitude);
        }.bind(this));

        
        this.shopList.sort(function(a,b){
          return a.latlongaddress - b.latlongaddress
        })
        this.setData({
          shoplistData:this.shopList,
        });
      }.bind(this),
     });
  },
  /**
   * 
   * @param {点击店铺获取店铺位置，打开腾讯地图跳转app} e 
   */
  _gotoAddres:function(e){
    console.log(e);
    const latitude = parseFloat(e.currentTarget.dataset.latitude);
    const longitude = parseFloat(e.currentTarget.dataset.longitude);
    wx.openLocation({
      latitude,
      longitude,
      scale: 18,
      name:e.currentTarget.dataset.shopname,
      address:e.currentTarget.dataset.address,
    })
  },
  /**
   * 
   * @param {计算两个经纬度的距离 km} e 
   */
  getaddreskm:function(lat1, lng1, lat2, lng2){
    var radLat1 = lat1*Math.PI / 180.0;
    var radLat2 = lat2*Math.PI / 180.0;
    var a = radLat1 - radLat2;
    var b = lng1*Math.PI / 180.0 - lng2*Math.PI / 180.0;
    var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) +
    Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
    s = s *6378.137 ;// EARTH_RADIUS;
    s = Math.round(s * 10000) / 10000;
    return s;

    
  },
  
  /**
   * 动画属性
   */
  // 动画函数
  chooseSezi: function (e) {
  

    // 用that取代this，防止不必要的情况发生
    var that = this;
    // 创建一个动画实例
    var animation = wx.createAnimation({
      // 动画持续时间
      duration: 900,
      // 定义动画效果，当前是匀速
      timingFunction: 'linear'
    })
    // 将该变量赋值给当前动画
    that.animation = animation
    // 先在y轴偏移，然后用step()完成一个动画
    if(this.data.chooseSize == false){
      animation.translateY(-345).step()
      // 用setData改变当前动画
      that.setData({
        // 通过export()方法导出数据
        animationData: animation.export(),
        // 改变view里面的Wx：if
        chooseSize: true,
        //加载透明层
        background_back:true,
      })
      // 设置setTimeout来改变y轴偏移量，实现有感觉的滑动 滑动时间
      setTimeout(function () {
        animation.translateY(0).step()
        that.setData({
          animationData: animation.export(),
          clearcart: false,
        })
      }, 100);

      //开启监听商店列表高度
      //调用之前关闭
      clearInterval(this.SetIntNumberUserInfo_One);  //关闭 铺获
      //开启定时器 铺获
      this._MonitorPage('#shop_list');
    }else{
      animation.translateY(-680).step()
      that.setData({
        animationData: animation.export()
      })
      setTimeout(function () {
        animation.translateY(0).step()
        that.setData({
          animationData: animation.export(),
          chooseSize: false,
          background_back:false,//清除透明层
          slider:1,//清空筛选值
          chooseId:false,//清空筛选值
        })
      }, 500)
    }
    
  },
  /**
   * 点击下滑弹出滑块 萌店类型
   */
  chooseSort:function(e){
     // 用that取代this，防止不必要的情况发生
     var that = this;
     // 创建一个动画实例
     var animation = wx.createAnimation({
       // 动画持续时间
       duration: 900,
       // 定义动画效果，当前是匀速
       timingFunction: 'linear'
     })
     // 将该变量赋值给当前动画
     that.animation = animation
     // 先在y轴偏移，然后用step()完成一个动画
     if(this.data.chooseSort == false){
      animation.translateY(-40).step()
        // 用setData改变当前动画
        that.setData({
          // 通过export()方法导出数据
          animationSort: animation.export(),
          // 改变view里面的Wx：if
          chooseSort: true,
          priceStutas:false
        })
        // 设置setTimeout来改变y轴偏移量，实现有感觉的滑动 滑动时间
        setTimeout(function () {
          animation.translateY(0).step()
          that.setData({
            animationSort: animation.export(),
            clearcart: false,
          })
        }, 100)
      }else{
        var animation = wx.createAnimation({
          // 动画持续时间
          duration: 300,
          // 定义动画效果，当前是匀速
          timingFunction: 'linear'
        })
        animation.translateY(-40).step()
        that.setData({
          animationSort: animation.export()
        })
        setTimeout(function () {
          animation.translateY(0).step()
          that.setData({
            animationSort: animation.export(),
            chooseSort: false,
            priceStutas:true
          })
        }, 500)
      }
  },
  /**
   * 
   * @param {
    *监听基本心动列表 页面长度
    *page : 监听页面id元素
    *LogSetInterval: 初始化巡航铺获
    *LogSetIntNum: 记录巡航铺获15次退出
    *}
    */
   _MonitorPage:function(page,LogSetIntNum = 15,LogSetInterval = new Array()){
     this.SetIntNumberUserInfo_One =  setInterval(function(param){ //巡航铺获
       wx.createSelectorQuery().select(page).boundingClientRect(function(res){
         if(res == null){
           console.log(res);
           clearInterval(this.SetIntNumberUserInfo_One);  //关闭 铺获
           return;
         }
         let heightView = res.height + 300;
         if(app.globalData.windowHeight >= heightView){
           this.setData({
            shop_height: (app.globalData.windowHeight + 250) +'px',
           });
           console.log(12);
         }else{
           this.setData({
             shop_height: (res.height + 250)+'px',
           });
         }
         console.log(this.data.shop_height);
         LogSetInterval.push(1);
         if(LogSetInterval.length == LogSetIntNum){
           clearInterval(this.SetIntNumberUserInfo_One);  //关闭 铺获
         }
       }.bind(this)).exec();
     }.bind(this),98);
   },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})